import yaml

'''
扁平化字典
pre: 前缀
cur_dict: 当前的字典
result: 扁平化的结果
'''
def flat_dict(pre, cur_dict, result):
  if isinstance(cur_dict, dict):
    for k,v in cur_dict.items():
      flat_dict(k if not pre else pre + '.' + k, v, result)
  else:
    result.append((pre, cur_dict))

with open("sample.yaml", 'r') as f:
  config = yaml.load(f, yaml.FullLoader)
  result = []
  flat_dict('', config, result)
  print(result)
